package com.sc.service.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sc.dal.mapper.member.MemberAddressMapper;
import com.sc.dal.mapper.member.RegionMapper;
import com.sc.dal.model.member.MemberAddress;
import com.sc.dal.model.member.Region;
import com.sc.util.data.Constants;

@Service
public class MemberAddressService {
	@Autowired
	private MemberAddressMapper memberAddressMapper;
	@Autowired
	private RegionMapper regionMapper;
	public List<MemberAddress> selectBymId(String mId){
		return memberAddressMapper.selectBymId(mId);
	}
	public MemberAddress selectAddressById(String maId){
		return memberAddressMapper.selectByPrimaryKey(maId);
	}
	public int addOrUpdateAddressById(MemberAddress memberAddress){
		if("1".equals(memberAddress.getIsdefault())){
			//修改其他地址为非默认地址
			memberAddressMapper.updateBymId(memberAddress);
		}
		if(memberAddress.getMaId()==null||"".equals(memberAddress.getMaId())){
			memberAddress.setCreatetime(new Date());
			memberAddress.setModifyTime(new Date());
			return memberAddressMapper.insertSelective(memberAddress);
		}else{
			memberAddress.setModifyTime(new Date());
			return memberAddressMapper.updateByPrimaryKeySelective(memberAddress);
		}
	}
	public List<?> selectAllRegion(){
		List<?> regionList=addRegionNode("0");
		return regionList;
	}
	private List<?> addRegionNode(String parentId){
		List<Region> regionList=regionMapper.selectRegionByFatherId(parentId);
		for (Region region : regionList) {
			if(region.getLevelId()==2){
				List<Region> regionList1=regionMapper.selectRegionByFatherId(region.getRegionCode());
				List<String> list=new ArrayList<String>();
				for (Region region2 : regionList1) {
					list.add(region2.getRegionName());
				}
				region.setAreaList(list);
			}else{
				region.setCityList(addRegionNode(region.getRegionCode()));
			}
		}
		return regionList;
	}
	public int deleteAddressById(String maId){
		MemberAddress memberAddress=new MemberAddress();
		memberAddress.setIsdelete(Constants.IS_DELETEB);
		memberAddress.setMaId(maId);
		return memberAddressMapper.updateByPrimaryKeySelective(memberAddress);
	}
	public MemberAddress selectDefaultAddress(String mId) {
		// TODO Auto-generated method stub
		return memberAddressMapper.selectDefaultAddress(mId);
	}
	public MemberAddress selectChoiceAddress(String maId) {
		return memberAddressMapper.selectChoiceAddress(maId);
	}
	public MemberAddress selectByPrimaryKey(String maId) {
		// TODO Auto-generated method stub
		return memberAddressMapper.selectByPrimaryKey(maId);
	}
	public List<Region> test001(){
		List<Region> list=regionMapper.selectAllRegion();
		
		return createRegionTree(list);
	}
	private List<Region> createRegionTree(List<Region> list){
		List<Region> data=new ArrayList<Region>();
		Map<String, List<String>> areaMap=new HashMap<String, List<String>>();
		Map<String, List<Region>> cityMap=new HashMap<String, List<Region>>();
		Map<String, List<Region>> provinceMap=new HashMap<String, List<Region>>();
		for (Region region : list) {
			if(region.getLevelId()==3){
				List<String> areas=areaMap.get(region.getParentId());
				if(areas==null){
					areas=new ArrayList<String>();
				}
				areas.add(region.getRegionName());
				areaMap.put(region.getParentId(), areas);
			}else if(region.getLevelId()==2){
				List<Region> citys=cityMap.get(region.getParentId());
				if(citys==null){
					citys=new ArrayList<Region>();
				}
				citys.add(region);
				cityMap.put(region.getParentId(), citys);
			}else{
				List<Region> provinces=provinceMap.get(region.getParentId());
				if(provinces==null){
					provinces=new ArrayList<Region>();
				}
				provinces.add(region);
				provinceMap.put(region.getParentId(), provinces);
			}
		}
		Set<String> citySet=cityMap.keySet();
		Set<String> provinceSet=provinceMap.keySet();
		for (String string : citySet) {
			List<Region> cityList=cityMap.get(string);
			for (Region region : cityList) {
				region.setAreaList(areaMap.get(region.getRegionCode()));
			}
		}
		for (String string : provinceSet) {
			List<Region> provinceList=provinceMap.get(string);
			for (Region region : provinceList) {
				region.setCityList(cityMap.get(region.getRegionCode()));
				data.add(region);
			}
		}
		
		return data;
	}
	public List<Region> selectAllRegionPc(){
		List<Region> list=regionMapper.selectAllRegion();
		
		return createRegionTreePc(list);
	}
	private List<Region> createRegionTreePc(List<Region> list){
		List<Region> data=new ArrayList<Region>();
		Map<String, List<Region>> areaMap=new HashMap<String, List<Region>>();
		Map<String, List<Region>> cityMap=new HashMap<String, List<Region>>();
		Map<String, List<Region>> provinceMap=new HashMap<String, List<Region>>();
		for (Region region : list) {
			if(region.getLevelId()==3){
//				List<String> areas=areaMap.get(region.getParentId());
//				if(areas==null){
//					areas=new ArrayList<String>();
//				}
//				areas.add(region.getRegionName());
//				areaMap.put(region.getParentId(), areas);
				List<Region> areas=areaMap.get(region.getParentId());
				if(areas==null){
					areas=new ArrayList<Region>();
				}
				areas.add(region);
				areaMap.put(region.getParentId(), areas);
			}else if(region.getLevelId()==2){
				List<Region> citys=cityMap.get(region.getParentId());
				if(citys==null){
					citys=new ArrayList<Region>();
				}
				citys.add(region);
				cityMap.put(region.getParentId(), citys);
			}else{
				List<Region> provinces=provinceMap.get(region.getParentId());
				if(provinces==null){
					provinces=new ArrayList<Region>();
				}
				provinces.add(region);
				provinceMap.put(region.getParentId(), provinces);
			}
		}
		Set<String> citySet=cityMap.keySet();
		Set<String> provinceSet=provinceMap.keySet();
		for (String string : citySet) {
			List<Region> cityList=cityMap.get(string);
			for (Region region : cityList) {
				region.setAreaList(areaMap.get(region.getRegionCode()));
			}
		}
		for (String string : provinceSet) {
			List<Region> provinceList=provinceMap.get(string);
			for (Region region : provinceList) {
				region.setCityList(cityMap.get(region.getRegionCode()));
				data.add(region);
			}
		}
		
		return data;
	}

}
